/**
 * 
 */
package support.dao.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import support.dao.BookDao;
import support.domain.Book;

/**
 * 
 * @author Wangpeihu
 * @team [Never give up]
 * @since JDK1.6(建议)
 * @date 2010-2-9
 */

public class BookDaoImpl extends JdbcDaoSupport implements BookDao {

	/**
	 * 保存
	 */
	public void save(Book book) {
		String sql = "insert into book values(?,?,?)";
		getJdbcTemplate().update(sql,
				new Object[] { book.getId(), book.getName(), book.getPrice() });
	}

	/**
	 * 删除
	 */
	public void delete(Book book) {
		String sql = "delete from book where id = ?";
		getJdbcTemplate().update(sql, new Object[] { book.getId() });
	}

	/**
	 * 查找所有
	 */
	@SuppressWarnings("unchecked")
	public List<Book> findAllBook() {
		String sql = "select * from book";
		return getJdbcTemplate().query(sql, new Object[] {}, new RowMapper() {
			public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
				Book book = new Book();
				book.setId(rs.getInt(1));
				book.setName(rs.getString(2));
				book.setPrice(rs.getDouble(3));
				return book;
			}
		});
	}

	/**
	 * 根据ID查找<br>
	 * 返回是一条记录用ResultSetExtractor
	 */
	public Book findBookById(Integer id) {
		String sql = "select * from book where id = ?";
		return (Book) getJdbcTemplate().query(sql, new Object[] { id },
				new ResultSetExtractor() {
					public Object extractData(ResultSet rs)
							throws SQLException, DataAccessException {
						if (rs.next()) {
							Book book = new Book();
							book.setId(rs.getInt(1));
							book.setName(rs.getString(2));
							book.setPrice(rs.getDouble(3));
							return book;
						}
						return null;
					}
				});
	}

	/**
	 * 根据价格区间查找<br>
	 * 返回结果是List用RowMapper
	 */
	@SuppressWarnings("unchecked")
	public List<Book> findBookByPrice(double from, double too) {
		String sql = "select * from book where price between ? and ?";
		return getJdbcTemplate().query(sql, new Object[] { from, too },
				new RowMapper() {
					// 这个方法被Spring调用，结果集有多少条记录就调用多少次，然后将返回的对象作为元素填充到List里面
					public Object mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						Book book = new Book();
						book.setId(rs.getInt(1));
						book.setName(rs.getString(2));
						book.setPrice(rs.getDouble(3));
						return book;
					}
				});
	}

	@SuppressWarnings("unchecked")
	public List<Book> findByPrice(double from, double to) {
		String sql = "select * from book where price between ? and ?";
		return getJdbcTemplate().query(sql, new Object[] { from, to },
				new RowMapper() {
					public Object mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						int id = rs.getInt(1);
						String name = rs.getString(2);
						double price = rs.getDouble(3);
						return new Book(id, name, price);
					}
				});
	}

	/**
	 * 更新
	 */
	public void update(Book book) {
		String sql = "update book set name=? , price=? where id=?";
		getJdbcTemplate().update(sql,
				new Object[] { book.getName(), book.getPrice(), book.getId() });
	}

}
